package com.yanghui.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.pojo.BlackIp;
import com.yanghui.service.BlackIpService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@RequestMapping("/admin")
@Controller
public class BlackIpControllerAdmin {

    @Resource(name = "blackIpServiceImpl")
    private BlackIpService blackIpService;

    /**
     * 将某个IP加入到黑名单
     * 注意需要考虑IP是否重复
     */
    @RequestMapping("/addBlackIp")
    @ResponseBody
    public Map addBlackIp(BlackIp blackIp) {
        Map<String,Object> map = new HashMap<>();
        int count = blackIpService.addBlackIp(blackIp);

        if (count == 1) {
            map.put("status", 200);
        } else if (count == -1) {
            map.put("status", 550);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 按照访客ID删除黑名单
     */
    @RequestMapping("/deleteBlackIpByID")
    @ResponseBody
    public Map<String,Object> deleteBlackIpByID(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        /** 受影响的行数 */
        int count = blackIpService.deleteBlackIpByID(id);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 按照指定条件查询黑名单
     */
    @RequestMapping("/selectLikeBlackIpListByPage")
    @ResponseBody
    public Map selectLikeBlackIpListByPage(
            BlackIp blackIp,
           @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
           @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize,
           @RequestParam(value = "startTime") String startTime,
           @RequestParam(value = "endTime") String endTime) {
        /** 封装要查询的条件 */
        Map<String,Object> map = new HashMap<>();
        if (blackIp.getIp() != null && "".equals(blackIp.getIp()) != true) {
            map.put("ip", blackIp.getIp());
        }
        if (blackIp.getCity() != null && "".equals(blackIp.getCity()) != true) {
            map.put("city", blackIp.getCity());
        }
        if (blackIp.getPlatformType() != null && "".equals(blackIp.getPlatformType()) != true) {
            map.put("platformType", blackIp.getPlatformType());
        }
        if (blackIp.getBrowserType() != null && "".equals(blackIp.getBrowserType()) != true) {
            map.put("browserType", blackIp.getBrowserType());
        }
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (startTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }

        PageHelper.startPage(page, pageSize);
        List<BlackIp> blackIpList = blackIpService.selectLikeBlackIpListByPage(map);
        PageInfo pageInfo = new PageInfo(blackIpList);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("pageInfo", pageInfo);

        return resultMap;
    }

    /**
     * 统计黑名单数量
     */
    @RequestMapping("/selectAllBlackIpCount")
    @ResponseBody
    public Map selectAllBlackIpCount() {
        Map<String,Object> map = new HashMap<>();
        int count = blackIpService.selectAllBlackIpCount();
        map.put("count", count);

        return map;
    }
}
